
#include "Common/Math.h"

#include "ParameterManager.h"
#include "../CastingRoutines.h"

using namespace ttg_internal::utils;

extern "C"
{
	//Creates parameter set in requred set.
	__declspec(dllexport) ttg::math::uint64 __stdcall createParameterSet(const char *name,
																		 ttg::math::uint64 root)
	{
		return ParameterManager::getRef()->createSet(gcnew System::String(name),
													 root);
	}

	//Creates parameter set in root set.
	__declspec(dllexport) ttg::math::uint64 __stdcall createRootParameterSet(const char *name)
	{ return ParameterManager::getRef()->createSet(gcnew System::String(name)); }

	//Releases required parameter set.
	__declspec(dllexport) void __stdcall releaseParameterSet(ttg::math::uint64 ID)
	{ ParameterManager::getRef()->releaseSet(ID); }

	//Creates parameter in requred set.
	__declspec(dllexport) ttg::math::uint64 __stdcall createParameter(const char *name,
																	  const char *type,
																	  ttg::math::uint64 root)
	{
		return ParameterManager::getRef()->createParameter(gcnew System::String(name),
														   gcnew System::String(type),
														   root);
	}

	//Creates parameter in root set.
	__declspec(dllexport) ttg::math::uint64 __stdcall createRootParameter(const char *name,
																		  const char *type)
	{
		return ParameterManager::getRef()->createParameter(gcnew System::String(name),
														   gcnew System::String(type));
	}

	//Releases required parameter.
	__declspec(dllexport) void __stdcall releaseParameter(ttg::math::uint64 ID)
	{ ParameterManager::getRef()->releaseParameter(ID); }

	//Returns value of requred parameter.
	//Note: returned string should be deleted by releaseString()!
	__declspec(dllexport) char * __stdcall getParameterValue(ttg::math::uint64 ID)
	{
		Parameter ^p = ParameterManager::getRef()->findParameter(ID);
		if (p != nullptr)
		{
			System::String ^val = p->ValueGet(true);
			char *res = new char[val->Length + 1];
			for (int i = 0; i < val->Length; i++)
				res[i] = (char)val[i];
			res[val->Length] = '\0';
			return res;
		}
		else
			return NULL;
	}

	//Releases string, that was returned by UtilManager.dll.
	__declspec(dllexport) void __stdcall releaseString(char *value)
	{ SAFE_DELETE(value); }

	//Sets new value.
	__declspec(dllexport) void __stdcall setParameterValue(ttg::math::uint64 ID, const char *value)
	{
		Parameter ^p = ParameterManager::getRef()->findParameter(ID);
		if (p != nullptr)
			p->ValueSet(gcnew String(value));
	}

	//Sets or skips ReadOnly flag.
	__declspec(dllexport) void __stdcall setParameterReadOnly(ttg::math::uint64 ID, bool value)
	{
		Parameter ^p = ParameterManager::getRef()->findParameter(ID);
		if (p != nullptr)
			p->ReadOnly = value;
	}

	//Locks required parameter.
	__declspec(dllexport) void __stdcall lockParameter(ttg::math::uint64 ID)
	{
		Parameter ^p = ParameterManager::getRef()->findParameter(ID);
		if (p != nullptr)
			p->lock();
	}

	//UnLocks required parameter.
	__declspec(dllexport) void __stdcall unLockParameter(ttg::math::uint64 ID)
	{
		Parameter ^p = ParameterManager::getRef()->findParameter(ID);
		if (p != nullptr)
			p->unLock();
	}

	//Returns true, if parameter was modified since last get().
	__declspec(dllexport) bool __stdcall wasParameterModified(ttg::math::uint64 ID)
	{
		Parameter ^p = ParameterManager::getRef()->findParameter(ID);
		if (p != nullptr)
			return p->wasModified();
		else
			return false;
	}
}
